package hw_8_3.thread_demo3;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Teacher implements Runnable{
    private static int count = 50;//总的试卷数量
    private static final Lock lock = new ReentrantLock();//锁对象
    private String name;

    public Teacher() {
    }

    public Teacher(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        while (true) {
            lock.lock();
            try {
                if (count <= 0){
                    break;
                }
                //模拟分发试卷的过程
                System.out.println(name + "正在分发试卷：" + count);
                count--;
            } finally {
                lock.unlock();//释放锁
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
